package com.shenmazong.demo1907amongo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.demo1907amongo.mapper.ITbUserMapper;
import com.shenmazong.demo1907amongo.pojo.TbUser;
import com.shenmazong.demo1907amongo.service.ITbUserService;
import com.shenmazong.demo1907amongo.utils.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2021/12/8 10:48
 */

@Service
@Slf4j
public class TbUserService implements ITbUserService {

    @Autowired
    ITbUserMapper iTbUserMapper;

    @Autowired
    MongoTemplate mongoTemplate;


    @Override
    public ResultResponse add(TbUser tbUser) {

        log.info("insert before" + tbUser);

        iTbUserMapper.insert(tbUser);

        log.info("insert after" + tbUser);

        // 方法1：新增加的数据，写入到mongodb
        TbUser user = iTbUserMapper.selectById(tbUser.getUserId());
        user.setMongoFlag(1);
        mongoTemplate.save(user);

        // 数据库回写
        iTbUserMapper.updateById(user);

        // 方法2：定时同步

        return ResultResponse.SUCCESS(user);
    }

    /**
     * @description 分页获取用户信息
     * @author 军哥
     * @date 2021/12/8 16:18
     * @version 1.0
     */
    @Override
    public ResultResponse getList(Integer pageSize, Integer pageNum) {

        if(pageNum <=0) {
            pageNum = 1;
        }
        if(pageSize <=0 || pageSize>100) {
            pageSize = 10;
        }

        Query query = new Query();
        Criteria criteria =new Criteria();

        // 查询记录
        long count = mongoTemplate.count(query, TbUser.class);

        // 计算分页
        // 计算偏移量
        Integer offset = (pageNum - 1)*pageSize;

        // 共有多少页
        Long pageCount = (count%pageSize>0)?(count/pageSize+1):(count/pageSize);

        // 存放结果
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("pageCount", pageCount);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);

        // 获取数据
        // 排序
        query.with(Sort.by(Sort.Order.desc("createTime")));
        query.skip(offset).limit(pageSize);
        List<TbUser> tbUsers = mongoTemplate.find(query, TbUser.class);

        map.put("users", tbUsers);

        return ResultResponse.SUCCESS(map);
    }

    @Override
    public ResultResponse getListByPage(Integer pageSize, Integer pageNum) {

        Page page = new Page(pageNum, pageSize);

        IPage iPage = iTbUserMapper.selectPage(page, new QueryWrapper<TbUser>().lambda().eq(TbUser::getMongoFlag, 1));

        return ResultResponse.SUCCESS(iPage);
    }

    @Override
    public ResultResponse getListByPageVo(Integer pageSize, Integer pageNum) {
        Page page = new Page(pageNum, pageSize);
        IPage<TbUser> tbUserIPage = iTbUserMapper.selectPageVo(page, 1);

        return ResultResponse.SUCCESS(tbUserIPage);
    }

    /**
     * @description 登录功能
     * @author 军哥
     * @date 2021/12/13 15:47
     * @version 1.0
     */
    @Override
    public ResultResponse login(String username, String password) {

        //--1
        TbUser tbUser = iTbUserMapper.selectOne(new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName, username).last("limit 1"));
        if(tbUser == null) {
            return ResultResponse.FAILED(404, "账号不存在");
        }

        //--2
        if(!tbUser.getUserPass().equals(password)) {
            return ResultResponse.FAILED(401, "密码错误");
        }

        return ResultResponse.SUCCESS(tbUser);
    }
}
